package com.mall.product.api.dao;

import com.mall.model.ProductBundle;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProductBundleMapper {

    /**
     * 插入商品组合
     */
    @Insert({
            "INSERT INTO product_bundle (name, description, price, status, create_time, update_time)",
            "VALUES (#{name}, #{description}, #{price}, #{status}, #{createTime}, #{updateTime})"
    })
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(ProductBundle productBundle);

    /**
     * 根据ID查询商品组合
     */
    @Select("SELECT * FROM product_bundle WHERE id = #{id}")
    ProductBundle selectById(Long id);

    /**
     * 查询所有商品组合
     */
    @Select("SELECT * FROM product_bundle ORDER BY create_time DESC")
    List<ProductBundle> selectAll();

    /**
     * 更新商品组合
     */
    @Update({
            "UPDATE product_bundle SET",
            "name = #{name},",
            "description = #{description},",
            "price = #{price},",
            "status = #{status},",
            "update_time = #{updateTime}",
            "WHERE id = #{id}"
    })
    int update(ProductBundle productBundle);

    /**
     * 删除商品组合
     */
    @Delete("DELETE FROM product_bundle WHERE id = #{id}")
    int deleteById(Long id);

    /**
     * 根据状态查询商品组合
     */
    @Select("SELECT * FROM product_bundle WHERE status = #{status} ORDER BY create_time DESC")
    List<ProductBundle> selectByStatus(Integer status);



    /**
     * 分页查询商品组合列表
     */
    @Select("<script>" +
            "SELECT * FROM product_bundle " +
            "WHERE 1=1 " +
            "<if test='keyword != null and keyword != \"\"'>" +
            "AND name LIKE CONCAT('%', #{keyword}, '%') " +
            "</if>" +
            "<if test='status != null'>" +
            "AND status = #{status} " +
            "</if>" +
            "ORDER BY create_time DESC " +
            "LIMIT #{offset}, #{limit}" +
            "</script>")
    List<ProductBundle> selectByQuery(@Param("keyword") String keyword,
                                      @Param("status") Integer status,
                                      @Param("offset") Integer offset,
                                      @Param("limit") Integer limit);


}